package routes

import (
	"net/http"
	"time"
	"web-app/controller"
	"web-app/logger"

	"github.com/gin-gonic/gin"

	_ "web-app/docs" // 千万不要忘了导入把你上一步生成的docs

	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"

	"github.com/gin-contrib/pprof"

)

func SetupRouter() *gin.Engine {

	// gin.SetMode(gin.ReleaseMode)
	r := gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true), RateLimitMiddleware(2*time.Second, 10))
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "OK")
	})
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	v1 := r.Group("/api/v1")

	v1.POST("/signup", controller.SignUpHandler)
	v1.POST("/login", controller.LoginHandler)

	v1.Use(JWTAuthMiddleware())
	{
		v1.GET("/community", controller.CommunityHandler)
		v1.GET("/community/:id", controller.CommunityDetailHandler)
		v1.POST("/post", controller.CreatePostHandler)
		v1.GET("/post/:id", controller.GetPostDetailHandler)
		//根据时间
		// v1.GET("/post/", controller.GetPostListHandler)
		// 根据分数
		v1.GET("/post2/", controller.GetPostListHandler2)
		v1.POST("/vote", controller.PostVoteController)
	}
	pprof.Register(r)//注册pprof相关路由
	r.NoRoute(func(c *gin.Context) {

		c.String(http.StatusOK, "404")
	})
	return r

}
